#include "shelldialog.h"
#include "ui_shelldialog.h"
#include "serialthread.h"
#include "quiwidget.h"

#include <QKeyEvent>
#include <QTextCodec>

ShellDialog::ShellDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ShellDialog)
{
    ui->setupUi(this);
    this->setWindowTitle("shell window");
    ui->shellTextEdit->installEventFilter(this);

    connect(SerialThread::Instance(), &SerialThread::SignalShellRecv, this, &ShellDialog::SlotShellRecv);
    connect(this, &ShellDialog::SignalSerialWriteStr,
            SerialThread::Instance(), &SerialThread::SlotSerialWriteStr, Qt::BlockingQueuedConnection);
}

ShellDialog::~ShellDialog()
{
    delete ui;
}

void ShellDialog::on_clrBtn_clicked()
{
    ui->shellTextEdit->clear();
}

void ShellDialog::SlotShellRecv(const QByteArray& str)
{
#if 0
    QTextCodec *utf8 = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(utf8);
    //QTextCodec::setCodecForCStrings(utf8);
    QTextCodec* gbk = QTextCodec::codecForName("gbk");

    //gbk -> utf8
    QString strUnicode = gbk->toUnicode(str.data());
    QByteArray utf8_bytes=utf8->fromUnicode(strUnicode);
    QString tmp1 = QString(utf8_bytes);

    //1. utf8 -> unicode QString str1
    //QString strUnicode= utf8->toUnicode(str1.toLocal8Bit().data());
    //2. unicode -> gbk, 得到QByteArray
    //QByteArray gb_bytes= gbk->fromUnicode(strUnicode);
#else
    QString tmp1 = QUIHelper::getCorrectUnicode(str);
#endif
    tmp1 = tmp1.replace("\r\n", "\n");
    ui->shellTextEdit->append(tmp1);
}

bool ShellDialog::eventFilter(QObject *target, QEvent *event)
{
    if(target == ui->shellTextEdit)
    {
        if(event->type() == QEvent::KeyRelease)//回车键
        {
            QString tmp;
            QKeyEvent *k = static_cast<QKeyEvent *>(event);
            int keyVal = k->key();

             //Key_Enter是小键盘的确认键，Key_Return是大键盘的回车键。
             if(keyVal == Qt::Key_Enter || keyVal == Qt::Key_Return)
             {    
                if(!m_input.isEmpty()) tmp.append(m_input);
                tmp = tmp.trimmed();
                tmp.append("\n");
                emit SignalSerialWriteStr(tmp);

                m_input.clear();

                return true;
             }

             //quint32 keyb = k->nativeVirtualKey();
             //quint32 keybb = k->nativeScanCode();
             QString keybbbb = k->text();
             if(isalnum(keyVal)
                || (keyVal == ' ') || (keyVal == '.') || (keyVal == '_')
                || (keyVal == '-') || (keyVal == '\"') || (keyVal == '\'')) {
                 //tmp = QString("%1").arg(keyVal);
                 //emit SignalSerialWriteStr(tmp);
                 m_input.append(keybbbb);

                 return true;
             }

             if(keyVal == Qt::Key_Backspace) {
                if(m_input.length() > 0) m_input = m_input.left(m_input.length() - 1);

                return true;
             }
        }
    }

    return QDialog::eventFilter(target, event);
}
